package jrain.iot.server.mqtt.service;

import java.util.concurrent.atomic.AtomicBoolean;

import jrain.fw.core.service.DestroyService;
import jrain.fw.core.service.InitService;
import jrain.fw.core.service.Service;

@Service(group = "collect.mqtt.MqttServerService", module = "collect")
public class MqttServerService implements InitService, DestroyService {

	private static final AtomicBoolean inited = new AtomicBoolean(false);
	private static volatile MqttNettyServer nettyServer;

	@Override
	public void init() {
		if (!inited.getAndSet(true)) {
			nettyServer = new MqttNettyServer();
			new Thread(() -> {
				nettyServer.start();
			}).start();
		}
	}

	@Override
	public void destroy() {
		if (inited.get()) {
			nettyServer.stop();
			inited.set(false);
		}
	}

}
